<template>
  <div class="content">
    <!--  生成凭证弹窗  -->
    <el-dialog
      v-dialogDrag
      title="生成凭证"
      model
      :visible.sync="isclientlDialog"
      :close-on-click-modal="false"
      width="500px"
      top="30vh"
      :before-close="handleClose"
    >
      <div>
        <span>选择模板</span>
        <el-select
          v-model="fmouldId"
          style="width: 300px;margin-left: 10px;"
          size="mini"
        >
          <el-option
            v-for="item in materData"
            :key="item.fmouldId"
            :label="item.ftemplateName + item.fdescription"
            :value="item.fmouldId"
          />
        </el-select>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="confirmPz">{{ $t('btn.confirm') }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  queryMouldList, createVouchers
} from '@/api/financial/voucher'

export default {
  props: {
    fromBillNo: {
      type: [String, Number],
      default: ''
    },
    fid: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      isclientlDialog: true,
      fmouldId: 0, // 模板ID
      materData: []
    }
  },
  created() {
    this.queryTBdCustomerList()
  },
  methods: {
    // 获取模板
    async queryTBdCustomerList() {
      this.isclientlDialog = true
      const DATA = {
        pageNum: 1,
        pageSize: 500
      }
      const { data: RES } = await queryMouldList(DATA)
      this.materData = RES.list
      this.fmouldId = RES.list[0].fmouldId
    },
    // 生成凭证
    confirmPz() {
      const DATA = {
        fmouldId: this.fmouldId,
        fid: this.fid,
        fromBillNo: this.fromBillNo
      }
      createVouchers(DATA).then(res => {
        if (res.code === 0) {
          this.handleClose()
          this.$message({ showClose: true, message: res.message, type: 'success' })
          this.$router.push({ path: `/certificateDetail/${res.data.voucherId}` })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 关闭前回调
    handleClose() {
      this.$emit('proof', this.prodValue)
    }
  }
}
</script>

<style scoped lang="scss">
.content {
  @include listBom;
}
</style>
